﻿using System.Collections;
using System.Collections.Generic;
using Entitas;
using UnityEngine;

public class GameBoardSystem : ReactiveSystem<GameEntity>,IInitializeSystem
{
//    public void Initialize()
//    {
//        
//        var gameBoard = CreateService.Instance.CreateGameBoard().componentsGameBoardComponents;
//
//        for (int i = 0; i < gameBoard.columns;i++)
//        {
//            for (int j = 0; j < gameBoard.raws; j++)
//            {
//                customData index = new customData();
//                index.x = i;
//                index.y = j;
//                
//                if (RandomNum() == true)
//                {
//                    CreateService.Instance.CreateBlock(index);
//                }
//                else
//                {
//                    CreateService.Instance.CreateRandomItem(index);
//                }
//            }
//        }
//    }

    public void Initialize()
    {
        var gameBoard = CreateService.Instance.CreateGameBoard()
            .componentsGameBoardComponents;
        var list = GetDataList();
        for (int row = 0; row < gameBoard.raws; row++)
        {
            for (int index = 0; index < list[row].Count; index++)
            {
                CreateService.Instance
                    .CreateItem(list[row][index],index,row);
            }
        }
    }



    public List<List<int>> GetDataList()
    {
        
        var model = Models.Instance.DataModel.Level[0];
        List<List<int>> list=new List<List<int>>();
        list.Add(model.row_0);
        list.Add(model.row_1);
        list.Add(model.row_2);
        list.Add(model.row_3);
        list.Add(model.row_4);
        list.Add(model.row_5);
        list.Add(model.row_6);
        list.Add(model.row_7);
        list.Add(model.row_8);
        return list;
    }
    

    private bool RandomNum()
    {
        int num = Random.Range(0, 100);
        if (num < 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public GameBoardSystem(Contexts context) : base(context.game)
    {
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsGameBoardComponents);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasComponentsGameBoardComponents;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (GameEntity entity in entities)
        {
            //TODO:消除组件
        }
    }
}
